<?php
session_start();

include("config.php");

$erreur = 0;
$form = 1;


//On se connecte à la base de donnée
$link = mysql_connect ('localhost', DB_USER_NAME, DB_PASSWORD) or die ("Cannot join database");
mysql_select_db (DB_NAME);


if(isset($_SESSION['pseudo']) && isset($_GET['id']))
{

	//On verifie si l'on a un premier acces à la page
	if(isset($_POST['text']))
	{
		//On verifie que tous les champs soient remplis
		if(empty($_POST['resume']) || empty($_POST['text'])  )
		{
			$erreur = 1;
			echo "Empty fields are not allowed <br>";
		}
		else
		{	
			//On affecte la valeur des champs à nos variables
			$resume = $_POST['resume'];
			$text = $_POST['text'];
			$id = $_POST['id'];

			$requete = "SELECT * FROM stream WHERE id_stream='" . $id . "'";
			$result = mysql_query ($requete);

			if(mysql_num_rows($result) == 0)
			{
				echo "This stream doesn't exist<br>";
			}
			else
			{
				$requete = "UPDATE stream SET resume='" . $_POST['resume'] . "', message='" . $_POST['text'] . "' WHERE id_stream='" . $id . "'";
				$result = mysql_query ($requete);
				echo "Stream added <br>";
				$form = 0;
			}

			
		
		}
	}
}
else
{
	echo "You must be connected <br>";
	$form = 0;	
}



if($form)
{
	$requete = "SELECT * FROM stream WHERE id_stream='" . $_GET['id'] . "' AND user= '" . $_SESSION['pseudo'] . "'";
	$result = mysql_query ($requete);	

	while ($row = mysql_fetch_array($result)) {
		$resume = $row['resume'];
		$text = $row['message'];
	}

}

mysql_close($link);		


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
	<head>
		<title>Life Stream - Edit a stream</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	</head>
   
	<body>		
		<?php
		if($form)
		{
		?>
				<form method="post" action="edit_stream.php?id=<? echo $_GET['id']; ?>">
				<p>Resume: <input type="text" name="resume" value="<? echo $resume; ?>"/></p>
				<p>Text:</p> <textarea type="textarea" name="text" cols="50" rows="10"><? echo $text; ?></textarea>
				<input type="hidden" name="id" value="<? echo $_GET['id']; ?>"> 
				<p><input type="submit" value="Valider" /></p>
				</form>
		<?
		}
		?>
	
	</body>
</html>
